﻿using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using Secured3d.Auto.Pages;
using Secured3d.Auto.Utils;

namespace Secured3d.Auto.Helpers
{
    public class PrintersBlock : BasePage
    {
        //locators

        [FindsBy(How = How.Id, Using = "add_new_printer_button")] 
        private IWebElement newPrinterButton = null;

        [FindsBy(How = How.CssSelector, Using = "#myTable3 caption .label-fix")] 
        private IWebElement printerStatusLabel = null;

        [FindsBy(How = How.CssSelector, Using = "#block div")] 
        private IWebElement printersAreAbsentLabel = null;

        
        public PrinterWizzard AddPrinter()
        {
            newPrinterButton.WaitUntilVisible().Click();
            return new PrinterWizzard();
        }

        public string PrinterStatus
        {
            get
            {
                return printerStatusLabel.WaitUntilVisible().Text;
            }
        }

        public bool PrintersAreAbsent
        {
            get
            {
                var result = false;
                try
                {
                    if (printersAreAbsentLabel.Text.Contains("You don't have any active printer jobs"))
                        result = true;
                }
                catch (NullReferenceException exception)
                {
                    
                }
                return result;
            }
        }

    }

    public class PrinterWizzard : BasePage
    {
        //
        [FindsBy(How = How.CssSelector, Using = "input[value=\"local\"]")] 
        private IWebElement localPrinterRadio = null;

        [FindsBy(How = How.CssSelector, Using = "input[value=\"organization\"]")]
        private IWebElement organizationPrinterRadio = null;

        [FindsBy(How = How.Id, Using = "set_way")] 
        private IWebElement nextStepButton = null;

        [FindsBy(How = How.Id, Using = "access_code")] 
        private IWebElement accessCodeField = null;

        [FindsBy(How = How.CssSelector, Using = "input[value=\"Add printers\"]")] 
        private IWebElement addPrintersButton = null;

        [FindsBy(How = How.Id, Using = "organization_success")]
        private IWebElement successAddedPrinterLabel = null;

        [FindsBy(How = How.CssSelector, Using = "#organization_success button")] 
        private IWebElement closeAddedWindowButton = null;

        [FindsBy(How = How.Id, Using = "new_printer_errors")] 
        private IWebElement printerErrorsLabel = null;

        public PrinterWizzard Location(PrinterLocation type)
        {
            switch (type)
            {
                case PrinterLocation.MyLocal:
                    localPrinterRadio.WaitUntilVisible().Click();
                    break;
                case PrinterLocation.MyOranization_School_University:
                    organizationPrinterRadio.WaitUntilVisible().Click();
                    break;
                    
            }
            nextStepButton.Click();
            return this;
        }

        public PrinterWizzard WithAccessCode(string accessCode)
        {
            accessCodeField.WaitUntilVisible().SendKeys(accessCode);
            addPrintersButton.Click();
            return this;

        }

        public void CloseSuccessConfirmation()
        {
            if(successAddedPrinterLabel.WaitUntilVisible().Displayed)
                closeAddedWindowButton.Click();
            else
            {
                throw new Exception(printerErrorsLabel.Text);
            }
        }
        
    }

    public enum PrinterLocation
    {
        MyLocal
        ,MyOranization_School_University
        ,MyFriends
        ,SomeOther
    }
}
